package com.znz.service;

import com.znz.domain.Project;
import com.znz.domain.Tcustomer;
import com.znz.domain.Thospital;
import com.znz.util.common.Page;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: duyuxin
 * Date: 15-4-28
 * Time: 下午9:09
 * To change this template use File | Settings | File Templates.
 */
@Service
public class CustomerSerService {
    @Autowired CommonService commonService;

    //TODO  查看所有已经注册的客服账户列表
    /**
     * @param userName  账户名称
     * @return
     */
    public Page<Tcustomer> getCustomerServiceList(String userName,Page<Tcustomer> page){
        try{
            Map<String,Object> params=new HashMap<String, Object>();
            StringBuilder where=new StringBuilder();
            if(null!=userName){
                 where.append(" o.userName like :uerName ");
                 params.put("uerName","%"+userName+"%");
            }
            page.setTotal((int)Tcustomer.countTcustomers());
            List<Tcustomer> tcustomerList=Tcustomer.getTcustomerByWhere(where.toString(),params,page.getOffset(),page.getOffset()+page.getLimit());
            page.setRows(tcustomerList);
            return page;
        }catch (Exception e){
            e.printStackTrace();
        }
        return  page;
    }
     //TODO 根据部位查询项目
    /**
     * @param partId 部位ID
     * @return
     */
    public List<Project> getProjectListByPartId(Long partId){
        List<Project> projects=new ArrayList<Project>();
        if(null!=partId){
            projects=commonService.getProjectListByPart(partId);
        }
        return projects;
    }
    //TODO 保存客服信息
    /**
     * @param customerService 客服对象
     * @return
     */
    public boolean saveCustomerService(JSONObject customerService){
         Tcustomer tcustomer=this.getTcustomer(customerService);
         int indexNum=0;
         while(getObjectjson("project",customerService,indexNum)!=null){
             indexNum=indexNum+1;
         }
         String isGoodat=null;
         for(int i=0;i<indexNum;i++){
            Object key= customerService.get("project"+"["+i+"]");
            System.out.print(key);
         }
         if(null!=tcustomer){
            return true;
         }
         return  false;
    }

    private Object getObjectjson(String key, JSONObject customerService, int indexnum) {
        return customerService.get(key + "[" + indexnum + "]");
    }
    private Tcustomer getTcustomer(JSONObject jsonTcustomer){
        Tcustomer  tcustomer=new Tcustomer();
        tcustomer.setUserName(jsonTcustomer.getString("userName"));
        tcustomer.setPassword("123456");
        tcustomer.setUserAlias(jsonTcustomer.getString("userAlias"));
        tcustomer.setUserPhone(jsonTcustomer.getString("userPhone"));
        tcustomer.setPercentage(jsonTcustomer.getString("percentage"));
        tcustomer.setUserLevel(jsonTcustomer.getString("userLevel"));
        tcustomer.setState(0);
        tcustomer.setUpdate_time(null);
        tcustomer.setCreate_time(new Date());
        return tcustomer;
    }

    /**
     * 方法说明:删除客服
     * @param cId 客服ID
     * @return
     */
    public Boolean delCustomerService(Long cId){
        boolean flag=false;
        try {
            if(cId!=null){
                //TODO 删除客服信息(修改状态)
                flag=Tcustomer.delCustomerService(cId);
            }
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return flag;
    }
}
